package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.sg.own.entity.RegionSituation;
import com.xx.sg.own.service.RegionSituationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/regionSituation")
@Tag(name = "区域态势", description = "区域态势API")
public class RegionSituationController {

    @Autowired
    private RegionSituationService regionSituationService;

    @PostMapping("/page")
    @Operation(summary = "分页区域态势", description = "分页区域态势")
    public Map page(@RequestParam(defaultValue = "1") Integer current,
                    @RequestParam(defaultValue = "10") Integer size,
                    @ModelAttribute RegionSituation regionSituation) {
        LambdaQueryWrapper<RegionSituation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(regionSituation.getArea()), RegionSituation::getArea, regionSituation.getArea())
                .eq(StringUtils.isNotBlank(regionSituation.getRegion()), RegionSituation::getRegion, regionSituation.getRegion());

        Page<RegionSituation> page = new Page<>(current, size);
        regionSituationService.page(page, wrapper);

        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "成功");
        res.put("data", page);
        return res;
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "区域态势详情", description = "区域态势详情")
    public Map info(@PathVariable("id") String id) {
        RegionSituation regionSituation = regionSituationService.getById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "成功");
        res.put("data", regionSituation);
        return res;
    }

    @PostMapping("/add")
    @Operation(summary = "添加区域态势", description = "添加区域态势")
    public Map add(RegionSituation regionSituation) {
        regionSituationService.save(regionSituation);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

    @PostMapping("/modify")
    @Operation(summary = "修改区域态势", description = "修改区域态势")
    public Map modify(RegionSituation regionSituation) {
        regionSituationService.updateById(regionSituation);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

    @GetMapping("/del/{id}")
    @Operation(summary = "删除区域态势", description = "删除区域态势")
    public Map del(@PathVariable String id) {
        regionSituationService.removeById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }
}
